package com.tamartech.greendb.config;

import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import javax.swing.JList;

import org.apache.log4j.Logger;

import com.tamartech.bridge.util.OperatingSystemUtils;

// make this an interface later
public class Configuration
{
    private static Logger log = Logger.getLogger(Configuration.class);
    
    Properties localProps = null;
    
    private String programName;
    private String localPropertyFileName;
    
    private final static Color COLOUR_SELECTED_TEXT = new JList().getSelectionForeground();
    private final static Color COLOUR_UNSELECTED_TEXT = new JList().getForeground();
    private final static Color COLOUR_SELECTED_BACKGROUND = new JList().getSelectionBackground(); 
    private final static Color COLOUR_TRANSPARENT = new Color(0, 0, 0, 255);
    private final static Color COLOUR_EVEN_ROW = new Color(237, 243, 254);
    private final static Color COLOUR_ODD_ROW = Color.WHITE;
    
    public String getCurrencySymbol()
    {
//        return "L";
        return "\u00a3"; // pound sign
    }
    
    public Color getColourSelectedText()
    {
        return COLOUR_SELECTED_TEXT;
    }
    
    public Color getColourUnselectedText()
    {
        return COLOUR_UNSELECTED_TEXT;
    }
    
    public Color getColourSelectedBackground()
    {
        return COLOUR_SELECTED_BACKGROUND;
    }
    
    public Color getColourTransparent()
    {
        return COLOUR_TRANSPARENT;
    }
    
    public Color getColourEvenRow()
    {
        return COLOUR_EVEN_ROW;
    }
    
    public Color getColourOddRow()
    {
        return COLOUR_ODD_ROW;
    }
 
    
    public String getProgramName()
    {
        return programName;
    }

    public void setProgramName(String programName)
    {
        this.programName = programName;
    }
    
    public String getLocalPropertyFileName()
    {
        return localPropertyFileName;
    }

    public void setLocalPropertyFileName(String localPropertyFileName)
    {
        this.localPropertyFileName = localPropertyFileName;
    }

    public File getContractCVFile()
    {
        return new File (getContractCVFileName());
    }
    
    public File getPermanentCVFile()
    {
        return new File (getPermanentCVFileName());
    }
    
    public void setContractCVFile (File cvFile)
    {
        setContractCVFileName(cvFile.toString());
    }
    
    public void setPermanentCVFile (File cvFile)
    {
        setPermanentCVFileName (cvFile.toString());
    }

    // TODO should probably write a File/String converter, but these will do for now
    public String getContractCVFileName()
    {
        return getLocalProps().getProperty ("cvfile.contract", "/");
    }
    
    public String getPermanentCVFileName()
    {
        return getLocalProps().getProperty ("cvfile.permanent", "/");
    }
    
    public void setContractCVFileName (String cvFileName)
    {
        getLocalProps().setProperty ("cvfile.contract", cvFileName);
    }
    
    public void setPermanentCVFileName (String cvFileName)
    {
        getLocalProps().setProperty ("cvfile.permanent", cvFileName);
    }
    
    public String getDataDirectory()
    {
        return OperatingSystemUtils.getWorkingDirectory(programName);
    }
    
    private Properties getLocalProps()
    {
        if (localProps == null)
        {
            localProps = new Properties();
            
            String propsPath = getPropsPath();
            
            try
            {
                localProps.load(new FileInputStream (propsPath));
            }
            catch (FileNotFoundException e)
            {
                log.info("Configuration file not found: " + propsPath);
            }
            catch (IOException e)
            {
                log.error ("Error reading configuration file: " + propsPath, e);
            }
        }
        
        return localProps;
    }
    
    private String getPropsPath()
    {
        return getDataDirectory() + File.separator + localPropertyFileName;
    }
    
    public void save()
    {
        if (localProps != null)
        {
            String propsPath = getPropsPath();
            
            try
            {
                // make sure the dir exists
                new File (propsPath).getParentFile().mkdirs();
                localProps.store(new FileOutputStream (propsPath), "");
            }
            catch (FileNotFoundException e)
            {
                log.error ("Failed to write configuration file: " + propsPath, e);
            }
            catch (IOException e)
            {
                log.error ("Failed to write configuration file: " + propsPath, e);
            }
        }
    }
    
    
}
